package com.taikang.hmp.hservice.doctor.chineseevalmodel.evaOfTraChiMedicine.action.intface;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.hservice.doctor.chineseevalmodel.bean.Hdbzdcchmedicine;
import com.taikang.hmp.hservice.doctor.chineseevalmodel.evaOfTraChiMedicine.service.IEvaOfTraChiMedicineService;
import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.tools.json.JsonHelper;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.frame.common.web.session.AppSessionFactory;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.frame.login.action.intface.LoginAction;

public class EvaOfTraChiMedicineAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private static Logger logger = LoggerFactory.getLogger(EvaOfTraChiMedicineAction.class);

	private IEvaOfTraChiMedicineService evaOfTraChiMedicineService;
	@Override
	public String executeAction() {
		if("getQuestion".equals(requestDto.getMethod())){
			return getQuestion();
		}
		else if("saveQuestionrResult".equals(requestDto.getMethod())){
			return saveQuestionrResult();
		} 
		else if("getQuestionrResult".equals(requestDto.getMethod())){
			return getQuestionrResult();
		} 
		else if("getJqys".equals(requestDto.getMethod())){
			return getJqys();
		} 
		else if("getCorporeitySolar".equals(requestDto.getMethod())){
			logger.info("--【节气养生中获取节气和体质】: method getCorporeitySolar --");
			return getCorporeitySolar();
		} 
		else{
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}
	/**
	 * 节气养生中获取节气和体质
	 * @return
	 */
	private String getCorporeitySolar() {
		ResponseDto responseDto = new ResponseDto();
		Dto paramDto = null;
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultList = evaOfTraChiMedicineService.getCorporeitySolar(paramDto);
		if(!resultList.containsKey("corporeity")){
			responseDto.addErrorCode(ErrorConstant.HD_NOT_TEST);
			super.setDataMap(responseDto);
			return SUCCESS;
		}
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	public final String saveQuestionrResult(){
//		try {
			ResponseDto responseDto = new ResponseDto();
			//标志 属于哪种体质
			String bz="";
			//每一道题的得分
			String score="";
			Dto paramDtos = requestDto.getRequestlist().get(0);
			List<Dto> list=paramDtos.getAsList("requestlist");	
			for(int i=0;i<list.size();i++){
				Dto paramDto =list.get(i);
				bz=bz+paramDto.getAsString("bz")+",";
				String  num=paramDto.getAsString("score");
				score=score+num+",";
			}
			score=score.substring(0,score.length()-1 );
			bz=bz.substring(0,bz.length()-1 );
			Dto paramDto =null;
			paramDto=super.appendRequestBase(paramDto, requestDto);
			List<Dto> resultList = evaOfTraChiMedicineService.saveQuestionrResult(score,bz,paramDto);
			Dto resultDto = new BaseDto();
			resultDto.put("resultlist", resultList);
			responseDto.pushResultDto(resultDto);
			super.setDataMap(responseDto);
			return SUCCESS;
	}
	public final String getQuestion(){
		try {
			ResponseDto responseDto = new ResponseDto();
			//获取参数
			Dto paramDto = null;
			if(requestDto.getRequestlist()!=null && !requestDto.getRequestlist().isEmpty()){
				paramDto = requestDto.getRequestlist().get(0);
			}
			paramDto = super.appendRequestBase(paramDto, requestDto);
			logger.info("templateid: "+paramDto.getAsString("templateid"));
			List<Dto> resultList = evaOfTraChiMedicineService.getQuestion(paramDto.getAsString("templateid"));
			Dto resultDto = new BaseDto();
			resultDto.put("resultlist", resultList);
			responseDto.pushResultDto(resultDto);
			super.setDataMap(responseDto);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("errorInfo", "获取问卷失败");
			dataMap.put("success", false);
			return SUCCESS;
		}
    }
	public final String getQuestionrResult(){
		
		try {
			ResponseDto responseDto = new ResponseDto();
			Dto paramDto =null;
			paramDto=super.appendRequestBase(paramDto, requestDto);
			List<Dto> resultList = evaOfTraChiMedicineService.getQuestionrResult(paramDto);
			Dto resultDto = new BaseDto();
			resultDto.put("resultlist", resultList);
			responseDto.pushResultDto(resultDto);
			super.setDataMap(responseDto);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("errorInfo", "获取问卷失败");
			dataMap.put("success", false);
			return SUCCESS;
		}
		
	}
	public String getJqys(){
		ResponseDto responseDto = new ResponseDto();
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
			Hdbzdcchmedicine sdf = evaOfTraChiMedicineService.getJqys(paramDto);
			Dto resultDto = new BaseDto();
			resultDto.put("sdf", sdf);
			responseDto.pushResultDto(resultDto);
			super.setDataMap(responseDto);
			return SUCCESS;
		
	}
	public void setEvaOfTraChiMedicineService(
			IEvaOfTraChiMedicineService evaOfTraChiMedicineService) {
		this.evaOfTraChiMedicineService = evaOfTraChiMedicineService;
	}

}
